using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using ImageManipulation;

namespace ImageViewer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Open image to picture box using open file dialog box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "PNG (*.png)|*.png|GIF (*.gif)|*.gif";
            openFileDialog1.FilterIndex = 1;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (Stream s = openFileDialog1.OpenFile())
                {
                    try
                    {
                        pictureBox1.Image = Image.FromStream(s);
                        toolStripStatusLabel1.Text = pictureBox1.Image.PixelFormat.ToString();
                    }
                    catch (ArgumentException)
                    {
                        MessageBox.Show("Cannot open given file.");
                    }
                }
            }
        }

        /// <summary>
        /// Save image from picture box as a new file using save file dialog box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "PNG (*.png)|*.png";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = saveFileDialog1.FileName;
                pictureBox1.Image.Save(fileName, ImageFormat.Png);
            }
        }

        /// <summary>
        /// Shrink image based on settings on tool strip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void shrinkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
                return;

            int maxColors;
            int.TryParse(toolStripComboBox2.Text.ToString(), out maxColors);

            Quantizer q;
            switch (toolStripComboBox3.Text)
            {
                case "Octree":
                    q = new OctreeQuantizer(maxColors - 1, 8);
                    break;
                case "Median Cut":
                    q = new MedianCutQuantizer(maxColors);
                    break;
                default:
                    q = new MedianCutQuantizer(maxColors);
                    break;
            }

            pictureBox1.Image = q.Quantize(pictureBox1.Image);
        }

    }
}